import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login"),
    hidden: true,
  },
  {
    path: "/register",
    component: () => import("@/views/register"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/error/404"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/error/401"),
    hidden: true,
  },
  {
    path: "",
    component: Layout,
    redirect: "index",
    children: [
      {
        path: "index",
        component: () => import("@/views/bi/report/index"),
        name: "Index",
        meta: { title: "首页", icon: "dashboard", affix: true },
      },
    ],
  },
  {
    path: "/user",
    component: Layout,
    hidden: true,
    redirect: "noredirect",
    children: [
      {
        path: "profile",
        component: () => import("@/views/system/user/profile/index"),
        name: "Profile",
        meta: { title: "个人中心", icon: "user" },
      },
    ],
  },
  {
    path: "",
    component: Layout,
    hidden: true,
    redirect: "noredirect",
    children: [
      {
        path: "report",
        component: () => import("@/views/bi/report/index"),
        name: "report",
        meta: { title: "数据分析(BI)", icon: "user" },
      },
    ],
  },
  {
    path: "/HRAndContract",
    component: () => import("@/views/bi/HRAndContract/index"),
    hidden: true,
  },
  {
    path: "/HRAndContract/item",
    component: () => import("@/views/bi/HRAndContract/indexItem"),
    hidden: true,
  },
  {
    path: "/costsAndPayments",
    component: () => import("@/views/bi/costsAndPayments/index"),
    hidden: true,
  },
  {
    path: "/assetsAndMaterials",
    component: () => import("@/views/bi/assetsAndMaterials/index"),
    hidden: true,
  },
  {
    path: "/assetsAndMaterials/item",
    component: () => import("@/views/bi/assetsAndMaterials/indexItem"),
    hidden: true,
  },
  {
    path: "/sales",
    component: () => import("@/views/bi/sales/index"),
    hidden: true,
  },
  {
    path: "/sales/item",
    component: () => import("@/views/bi/sales/indexItem"),
    hidden: true,
  },
];

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: "/report",
    component: Layout,
    hidden: true,
    permissions: ["cld:financereportbi:list"],
    children: [
      {
        path: "financeReportBI",
        component: () => import("@/views/cld/financeReportBINew/index"),
        name: "financeReportBI",
        meta: {
          title: "运营与支付BI",
          activeMenu: "/report/financeReportBI",
          fullscreen: true,
        },
      },
      {
        path: "financeReportBIItme",
        component: () => import("@/views/cld/financeReportBINew/indexItem"),
        name: "financeReportBIItme",
        meta: {
          title: "运营与支付BI-项目",
          activeMenu: "/report/indexItem",
          fullscreen: true,
        },
      },
      {
        path: "humanResourcesBI",
        component: () => import("@/views/cld/humanResourcesBI/index"),
        name: "humanResourcesBI",
        meta: {
          title: "人资与履约BI",
          activeMenu: "/report/humanResourcesBI",
          fullscreen: true,
        },
      },
      {
        path: "materialsBI",
        component: () => import("@/views/erp/stashReportBI/index"),
        name: "materialsBI",
        meta: {
          title: "物资报表BI",
          activeMenu: "/report/materialsBI",
          fullscreen: true,
        },
      },
      {
        path: "materialsBIItem",
        component: () => import("@/views/erp/stashReportBI/indexItem"),
        name: "materialsBIItem",
        meta: {
          title: "物资报表BI-项目",
          activeMenu: "/report/materialsBIItem",
          fullscreen: true,
        },
      },
    ],
  },
  {
    path: "/system/user-auth",
    component: Layout,
    hidden: true,
    permissions: ["system:user:edit"],
    children: [
      {
        path: "role/:userId(\\d+)",
        component: () => import("@/views/system/user/authRole"),
        name: "AuthRole",
        meta: { title: "分配角色", activeMenu: "/system/user" },
      },
    ],
  },
  {
    path: "/system/role-auth",
    component: Layout,
    hidden: true,
    permissions: ["system:role:edit"],
    children: [
      {
        path: "user/:roleId(\\d+)",
        component: () => import("@/views/system/role/authUser"),
        name: "AuthUser",
        meta: { title: "分配用户", activeMenu: "/system/role" },
      },
    ],
  },
  {
    path: "/system/dict-data",
    component: Layout,
    hidden: true,
    permissions: ["system:dict:list"],
    children: [
      {
        path: "index/:dictId(\\d+)",
        component: () => import("@/views/system/dict/data"),
        name: "Data",
        meta: { title: "字典数据", activeMenu: "/system/dict" },
      },
    ],
  },
  {
    path: "/monitor/job-log",
    component: Layout,
    hidden: true,
    permissions: ["monitor:job:list"],
    children: [
      {
        path: "index/:jobId(\\d+)",
        component: () => import("@/views/monitor/job/log"),
        name: "JobLog",
        meta: { title: "调度日志", activeMenu: "/monitor/job" },
      },
    ],
  },
  {
    path: "/tool/gen-edit",
    component: Layout,
    hidden: true,
    permissions: ["tool:gen:edit"],
    children: [
      {
        path: "index/:tableId(\\d+)",
        component: () => import("@/views/tool/gen/editTable"),
        name: "GenEdit",
        meta: { title: "修改生成配置", activeMenu: "/tool/gen" },
      },
    ],
  },
  {
    path: "/ErpSupplier/set-commodity",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpSupplier:add"],
    children: [
      {
        path: "erpSupplier/:supplierId(\\d+)",
        component: () => import("@/views/erp/ErpSupplier/ErpSupplierCommodity"),
        name: "SupplierCommodity",
        meta: {
          title: "设置商品",
          activeMenu: "/erpSupplier/ErpSupplierCommodity",
        },
      },
    ],
  },
  {
    path: "/ErpProcure/add-procure",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpProcure:add"],
    children: [
      {
        path: "erpProcure/:procureId(\\d+)",
        component: () => import("@/views/erp/ErpProcure/addProcure"),
        name: "ErpProcure",
        meta: { title: "采购单", activeMenu: "/erpProcure/addProcure" },
      },
    ],
  },
  {
    path: "/ErpSale/add-sale",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpSale:add"],
    children: [
      {
        path: "erpSale/:saleId(\\d+)",
        component: () => import("@/views/erp/ErpSale/addSale"),
        name: "ErpSale",
        meta: { title: "销售单", activeMenu: "/ErpSale/addSale" },
      },
    ],
  },
  {
    path: "/ErpSaleReturn/add-saleReturn",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpSaleReturn:add"],
    children: [
      {
        path: "saleReturn/:saleReturnId(\\d+)",
        component: () => import("@/views/erp/ErpSaleReturn/addSaleReturn"),
        name: "ErpSaleReturn",
        meta: {
          title: "销售退换货",
          activeMenu: "/ErpSaleReturn/addSaleReturn",
        },
      },
    ],
  },
  {
    path: "/ErpProcudeReturn/add-procudeReturn",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpProcudeReturn:add"],
    children: [
      {
        path: "procudeReturn/:procudeReturnId(\\d+)",
        component: () =>
          import("@/views/erp/ErpProcudeReturn/addProcudeReturn"),
        name: "ErpProcudeReturn",
        meta: {
          title: "采购退换货",
          activeMenu: "/ErpProcudeReturn/addProcudeReturn",
        },
      },
    ],
  },
  {
    path: "/ErpWareAllocate/add-wareAllocate",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpWareAllocate:add"],
    children: [
      {
        path: "wareAllocate/:wareAllocateId(\\d+)",
        component: () => import("@/views/erp/ErpWareAllocate/addWareAllocate"),
        name: "ErpWareAllocate",
        meta: {
          title: "库存调拨单",
          activeMenu: "/ErpWareAllocate/addWareAllocate",
        },
      },
    ],
  },
  {
    path: "/ErpWareCheck/add-wareCheck",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpWareCheck:add"],
    children: [
      {
        path: "wareCheck/:wareCheckId(\\d+)",
        component: () => import("@/views/erp/ErpWareCheck/addWareCheck"),
        name: "ErpWareCheck",
        meta: {
          title: "库存盘点单",
          activeMenu: "/ErpWareCheck/addWareCheck",
        },
      },
    ],
  },
  {
    path: "/ErpWareReporting/add-addWareReporting",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpWareReporting:add"],
    children: [
      {
        path: "wareReporting/:wareReportingId(\\d+)",
        component: () =>
          import("@/views/erp/ErpWareReporting/addWareReporting"),
        name: "ErpWareReporting",
        meta: {
          title: "报损单",
          activeMenu: "/ErpWareReporting/addWareReporting",
        },
      },
    ],
  },
  {
    path: "/ErpWareOverflow/add-addWareOverflow",
    component: Layout,
    hidden: true,
    permissions: ["erp:ErpWareOverflow:add"],
    children: [
      {
        path: "wareOverflow/:wareOverflowId(\\d+)",
        component: () => import("@/views/erp/ErpWareOverflow/addWareOverflow"),
        name: "ErpWareOverflow",
        meta: {
          title: "报溢单",
          activeMenu: "/ErpWareOverflow/addWareOverflow",
        },
      },
    ],
  },
  {
    path: "/project/profitEstimation",
    component: Layout,
    hidden: true,
    permissions: ["cld:project:estimate"],
    children: [
      {
        path: "profitEstimation/:projectId(\\d+)",
        component: () => import("@/views/cld/project/profitEstimation"),
        name: "project",
        meta: {
          title: "利润估算",
          activeMenu: "/project/profitEstimation",
        },
      },
    ],
  },
  {
    path: "/CldAssetsRecord/add-assetsRecord",
    component: Layout,
    hidden: true,
    permissions: ["cld:CldAssetsRecord:add"],
    children: [
      {
        path: "recordList/:assetsId(\\d+)",
        component: () => import("@/views/cld/assets/record/index"),
        name: "CldAssetsRecord",
        meta: { title: "资产状况", activeMenu: "/CldAssetsRecord/index" },
      },
    ],
  },
  {
    path: "/CldAssetsDetails/assetsDetails",
    component: Layout,
    hidden: true,
    permissions: ["cld:assets:list"],
    children: [
      {
        path: "detailsList/:assetsId(\\d+)",
        component: () => import("@/views/cld/assets/details/index"),
        name: "CldAssetsRecord",
        meta: { title: "资产明细", activeMenu: "/CldAssetsDetails/index" },
      },
    ],
  },
  {
    path: "/project/add-workPlan",
    component: Layout,
    hidden: true,
    permissions: ["cld:project:addWorkPlan"],
    children: [
      {
        path: "workPlanList/:projectId(\\d+)",
        component: () => import("@/views/cld/project/addWorkPlan"),
        name: "project",
        meta: { title: "设置工作计划", activeMenu: "/project/addWorkPlan" },
      },
    ],
  },
  {
    path: "/project/add-group",
    component: Layout,
    hidden: true,
    permissions: ["cld:project:addGroup"],
    children: [
      {
        path: "groupList/:projectId(\\d+)",
        component: () => import("@/views/cld/project/addGroup"),
        name: "project",
        meta: { title: "设置工作小组", activeMenu: "/project/addGroup" },
      },
    ],
  },
  {
    path: "/project/add-projectAssets",
    component: Layout,
    hidden: true,
    permissions: ["cld:project:addProjectAssets"],
    children: [
      {
        path: "projectAssetsList/:projectId(\\d+)",
        component: () => import("@/views/cld/project/addProjectAssets"),
        name: "project",
        meta: {
          title: "设置项目资产",
          activeMenu: "/project/addProjectAssets",
        },
      },
    ],
  },
  {
    path: "/project/view-projectPost",
    component: Layout,
    hidden: true,
    permissions: ["cld:project:viewPost"],
    children: [
      {
        path: "viewProjectPost/:projectId(\\d+)",
        component: () => import("@/views/cld/project/viewProjectPost"),
        name: "project",
        meta: { title: "查看项目岗位", activeMenu: "/project/viewProjectPost" },
      },
    ],
  },
  {
    path: "/project/add-customerAccount",
    component: Layout,
    hidden: true,
    permissions: ["cld:project:addCustomerAccount"],
    children: [
      {
        path: "accountList/:projectId(\\d+)",
        component: () => import("@/views/cld/project/addCustomerAccount"),
        name: "project",
        meta: {
          title: "设置客户账号",
          activeMenu: "/project/addCustomerAccount",
        },
      },
    ],
  },
  {
    path: "/complaints/complaints",
    component: Layout,
    hidden: true,
    permissions: ["cld:complaints:details"],
    children: [
      {
        path: "detailsList/:projectId(\\d+)",
        component: () => import("@/views/cld/complaints/complaintsDetails"),
        name: "complaints",
        meta: {
          title: "客诉与处置",
          activeMenu: "/complaints/complaintsDetails",
        },
      },
    ],
  },
  {
    path: "/workPlan/workPlan",
    component: Layout,
    hidden: true,
    permissions: ["cld:workPlan:query"],
    children: [
      {
        path: "detailsList/:projectId(\\d+)",
        component: () => import("@/views/cld/workPlan/workPlanDetails"),
        name: "workPlan",
        meta: {
          title: "工作计划",
          activeMenu: "/workPlan/workPlanDetails",
        },
      },
    ],
  },
  {
    path: "/insurance/related",
    component: Layout,
    hidden: true,
    permissions: ["cld:insurance:related"],
    children: [
      {
        path: "personnel/:insuranceId(\\d+)",
        component: () => import("@/views/cld/insurance/relatedPersonnel"),
        name: "insurance",
        meta: {
          title: "关联人员",
          activeMenu: "/insurance/relatedPersonnel",
        },
      },
    ],
  },
  {
    path: "/staffContract/contract",
    component: Layout,
    hidden: true,
    permissions: ["cld:staffContract:details"],
    children: [
      {
        path: "details/:projectId(\\d+)",
        component: () => import("@/views/cld/staffContract/contractDetails"),
        name: "staffContract",
        meta: {
          title: "人事合同管理",
          activeMenu: "/staffContract/contractDetails",
        },
      },
    ],
  },
  {
    path: "/iecturer",
    component: Layout,
    hidden: true,
    permissions: ["cld:trainPlan:receive"],
    children: [
      {
        path: "receive/:trainPlanId(\\d+)",
        component: () => import("@/views/cld/iecturer/receive"),
        name: "iecturer",
        meta: {
          title: "培训计划",
          activeMenu: "/iecturer/receive",
        },
      },
    ],
  },
  {
    path: "/projectRepository/material",
    component: Layout,
    hidden: true,
    permissions: ["cld:projectRepository:materialDdetails"],
    children: [
      {
        path: "details/:projectId(\\d+)",
        component: () =>
          import("@/views/cld/projectRepository/materialDetails"),
        name: "projectRepository",
        meta: {
          title: "物资明细",
          activeMenu: "/projectRepository/materialDetails",
        },
      },
    ],
  },
  {
    path: "/projectRepository/teamMember",
    component: Layout,
    hidden: true,
    permissions: ["cld:projectRepository:teamMemberDetails"],
    children: [
      {
        path: "details/:projectId(\\d+)",
        component: () =>
          import("@/views/cld/projectRepository/teamMemberDetails"),
        name: "projectRepository",
        meta: {
          title: "组员明细",
          activeMenu: "/projectRepository/teamMemberDetails",
        },
      },
    ],
  },
  {
    path: "/asset/repair",
    component: Layout,
    hidden: true,
    permissions: ["cld:repair:detailsList"],
    children: [
      {
        path: "details/:assetsDetailsId(\\d+)",
        component: () => import("@/views/cld/assets/repair/repairDetails"),
        name: "assetRepair",
        meta: {
          title: "维修明细",
          activeMenu: "/asset/repair/details",
        },
      },
    ],
  },
  {
    path: "/asset/repair-cost",
    component: Layout,
    hidden: true,
    permissions: ["cld:repair:cost"],
    children: [
      {
        path: "cost/:assetsDetailsId(\\d+)",
        component: () => import("@/views/cld/assets/repair/repairCost"),
        name: "assetRepair",
        meta: {
          title: "维修成本",
          activeMenu: "/asset/repair/cost",
        },
      },
    ],
  },
  {
    path: "/asset/repair",
    component: Layout,
    hidden: true,
    permissions: ["cld:repair:add"],
    children: [
      {
        path: "addRepair/:assetsRecordId(\\d+)",
        component: () => import("@/views/cld/assets/repair/addRepair"),
        name: "assetRepair",
        meta: {
          title: "资产维修",
          activeMenu: "/asset/repair/addRepair",
        },
      },
    ],
  },
  {
    path: "/materialAllocate/add-allocate",
    component: Layout,
    hidden: true,
    permissions: ["cld:materialAllocate:add"],
    children: [
      {
        path: "details/:materialAllocateId(\\d+)",
        component: () => import("@/views/cld/materialAllocate/addAllocate"),
        name: "materialAllocate",
        meta: {
          title: "物资调拨",
          activeMenu: "/materialAllocate/addAllocate",
        },
      },
    ],
  },
  {
    path: "/materialConsume/add-consume",
    component: Layout,
    hidden: true,
    permissions: ["cld:materialConsume:add"],
    children: [
      {
        path: "addConsume/:materialConsumeId(\\d+)",
        component: () => import("@/views/cld/materialConsume/addConsume"),
        name: "materialConsume",
        meta: {
          title: "添加消耗记录",
          activeMenu: "/materialConsume/addConsume",
        },
      },
    ],
  },
  {
    path: "/restbreaks/add-scheduling",
    component: Layout,
    hidden: true,
    permissions: ["cld:restbreaks:add"],
    children: [
      {
        path: "addScheduling/:restbreakId(\\d+)",
        component: () => import("@/views/cld/restbreaks/addScheduling"),
        name: "restbreaks",
        meta: {
          title: "编辑排休",
          activeMenu: "/restbreaks/addScheduling",
        },
      },
    ],
  },
  {
    path: "/restbreaks/viewSchedule",
    component: Layout,
    hidden: true,
    permissions: ["cld:restbreaks:query"],
    children: [
      {
        path: "view/:restbreakId(\\d+)",
        component: () => import("@/views/cld/restbreaks/viewSchedule"),
        name: "restbreaks",
        meta: {
          title: "查看排休",
          activeMenu: "/restbreaks/viewSchedule",
        },
      },
    ],
  },
  {
    path: "/CldPayDetails/realTime",
    component: Layout,
    hidden: true,
    permissions: ["cld:restbreaks:add"],
    children: [
      {
        path: "regionDetails/:projectRegionId(\\d+)",
        component: () =>
          import("@/views/cld/CldPayDetails/realTime/regionDetails"),
        name: "CldPayDetails",
        meta: {
          title: "实时薪酬-项目",
          activeMenu: "/CldPayDetails/realTime/regionDetails",
        },
      },
    ],
  },
  {
    path: "/CldPayDetails/realTime",
    component: Layout,
    hidden: true,
    permissions: ["cld:restbreaks:add"],
    children: [
      {
        path: "projectDetails/:projectId(\\d+)",
        component: () =>
          import("@/views/cld/CldPayDetails/realTime/projectDetails"),
        name: "CldPayDetails",
        meta: {
          title: "实时薪酬-组员",
          activeMenu: "/CldPayDetails/realTime/projectDetails",
        },
      },
    ],
  },
  {
    path: "/CldPayDetails/cumulativeSalary",
    component: Layout,
    hidden: true,
    permissions: ["cld:restbreaks:add"],
    children: [
      {
        path: "projectDetails/:projectId(\\d+)",
        component: () =>
          import("@/views/cld/CldPayDetails/cumulativeSalary/projectDetails"),
        name: "CldPayDetails",
        meta: {
          title: "累计薪酬-项目",
          activeMenu: "/CldPayDetails/cumulativeSalary/projectDetails",
        },
      },
    ],
  },
  {
    path: "/attendance/viewAttendance",
    component: Layout,
    hidden: true,
    permissions: ["cld:attendance:query"],
    children: [
      {
        path: "view/:projectId(\\d+)",
        component: () => import("@/views/cld/attendance/viewAttendance"),
        name: "attendance",
        meta: {
          title: "查看考勤",
          activeMenu: "/attendance/viewAttendance",
        },
      },
    ],
  },
  {
    path: "/accountInfo/details/index",
    component: Layout,
    hidden: true,
    permissions: ["cld:accountDetails:query"],
    children: [
      {
        path: "view/:accountInfoId(\\d+)",
        component: () => import("@/views/cld/accountInfo/details/index"),
        name: "viewDetails",
        meta: {
          title: "查看账户明细",
          activeMenu: "/accountInfo/details/index",
        },
      },
    ],
  },
];

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push;
let routerReplace = Router.prototype.replace;
// push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((err) => err);
};
// replace
Router.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch((err) => err);
};

export default new Router({
  mode: "history", // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
});
